/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

function UserProfileViewModel() {
    var self = this;
    // put a property loop here
    self.UserId = ko.observable();
    self.UserName = ko.observable();
    self.TS = ko.observable();
    self.CreatedBy = ko.observable();
    self.CreatedDt = ko.observable();
    self.ModifiedBy = ko.observable();
    self.ModifiedDt = ko.observable();
    self.IsActive = ko.observable();
    self.FirstName = ko.observable();
    self.LastName = ko.observable();
    self.Address = ko.observable();
    self.City = ko.observable();
    self.Region = ko.observable();
    self.PostalCode = ko.observable();
    self.OfficePhone = ko.observable();
    self.CellPhone = ko.observable();
    self.Title = ko.observable();
    self.Action = ko.observable();
    self.Caller = ko.observable();
    self.Role = ko.observable();
    self.PageSize = ko.observable();
    self.PageNumber = ko.observable();
    self.SortColumn = ko.observable();
    self.SortType = ko.observable();
    self.TotalRows = ko.observable();

    self.UserProfileList = ko.observableArray([]);
    self.setUserProfile = function (
			UserId, UserName, TS, CreatedBy, CreatedDt, ModifiedBy, ModifiedDt, IsActive, FirstName, LastName, Address, City, Region, PostalCode, OfficePhone, CellPhone, Title, _Action, _Caller, _Role, _PageNumber, _PageSize, _SortColumn, _SortType, _TotalRows

	) {

        UserId = _UserId;
        UserName = _UserName;
        TS = _TS;
        CreatedBy = _CreatedBy;
        CreatedDt = _CreatedDt;
        ModifiedBy = _ModifiedBy;
        ModifiedDt = _ModifiedDt;
        IsActive = _IsActive;
        FirstName = _FirstName;
        LastName = _LastName;
        Address = _Address;
        City = _City;
        Region = _Region;
        PostalCode = _PostalCode;
        OfficePhone = _OfficePhone;
        CellPhone = _CellPhone;
        Title = _Title;
        Action = _Action;
        Caller = _Caller;
        Role = _Role;
        PageSize = _PageSize
        PageNumber = _PageNumber;
        SortColumn = _SortColumn;
        SortType = _SortType;
        TotalRows = _TotalRows;

    }; //  End of set procedure 

    //Add User Profile
    self.AddUserProfile = function () {

        alert("We are in the AddUserProfile ko function");

        this.Action = "Add";
        this.Caller = "trooks";
        this.Role = "user";
        var myData = ko.toJSON(this);
        jQuery.ajax({
            url: "/api/UserProfileapi/",
            type: 'post',
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
            contentType: 'application/json',
            success: function (result) {
                alert(result);
            }
        });



    }; //End of add procedure

    //load data User Profile
    self.LoadUserprofile = function (userVal) {
        self = userVal;
        alert("Load User " + self.UserId);

    };
    //Delete User Profile
    self.DeleteUserprofile = function () {
        //alert("Delete Action " + self.UserId);
        self.Action = "Delete";
        self.Caller = "trooks";
        self.Role = "user";
        self.CreatedBy = "trooks";
        self.CreatedDt = "06/22/2013";
        self.ModifiedBy = "trooks";
        self.ModifiedDt = "06/22/2013";


        jQuery.ajax({
            url: "/api/UserProfileapi/",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
            type: 'POST'


        }).done(function (data) {

            $.each(data, function (key, val) {
                alert("We have loaded the values and are in the each module. " + JSON.stringify(val));

            })
        })
    };

    //Edit User Profile
    self.UpdateUserprofile = function () {

        //alert("We are in the Edit User " + self.UserName);

        self.Action = "Load";
        self.Caller = "trooks";
        self.Role = "user";
        self.CreatedBy = "trooks";
        self.CreatedDt = "06/22/2013";
        self.ModifiedBy = "trooks";
        self.ModifiedDt = "06/22/2013";

        var myData = ko.toJSON(self);
        jQuery.ajax({
            url: "/api/UserProfileapi/",
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                alert(result);
            }
        });



    };

    //Sort user profile
    self.sortRecords = ko.observableArray();

    ko.bindingHandlers.sort = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var asc = false;
            element.style.cursor = 'pointer';

            element.onclick = function () {
                var value = valueAccessor();
                var prop = value.prop;
                var data = value.arr;

                asc = !asc;
                if (asc) {
                    self.UserProfileList.sort(function (left, right) {
                        return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
                    });
                } else {
                    self.UserProfileList.sort(function (left, right) {
                        return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
                    });
                }
            }
        }
    };


    self.GetUserProfileByParameter = function () {
        alert('u are in here');
        this.Action = "GetAll";
        this.Caller = "trooks";
        this.Role = "user";
        this.PageNumber = 1;
        this.PageSize = 10;
        this.SortColumn = "UserId";
        this.SortType = "asc";
        var myData = ko.toJSON(this);
        jQuery.ajax({
            url: "/api/UserProfileapi/",
            type: 'get',
            data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
        }).done(function (data) {

            $.each(data, function (key, val) {

                self.UserProfileList.push(
                    val
                )
            })
        });



    };


}; //End of view model





